home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / lang / classl~2.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.4 KB  |  146 lines

  1. /*
  2.  * @(#)ClassLoader.java    1.29 95/12/21 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.io.InputStream;
  23.  
  24. /**
  25.  * ClassLoader is an abstract Class that can be used to define a policy
  26.  * for loading Java classes into the runtime environment. By default,
  27.  * the runtime system loads classes that originate as files by reading 
  28.  * them from the directory defined by the <tt>CLASSPATH</tt> environment
  29.  * variable (this is platform dependent). The default mechanism does not involve
  30.  * a Class loader. <p>
  31.  *
  32.  * However, some classes may not originate from a file; they could be
  33.  * loaded from some other source, e.g., the network. Classes loaded
  34.  * from the network are an array of bytes. A ClassLoader can be used to
  35.  * tell the runtime system to convert an array of bytes into an instance
  36.  * of class Class.
  37.  * This conversion information is passed to the runtime using the defineClass()
  38.  * method.<p>
  39.  *
  40.  * Classes that are created through the defineClass() mechanism can 
  41.  * reference other classes by name. To resolve those names, the runtime
  42.  * system calls the ClassLoader that originally created the Class.
  43.  * The runtime system calls the abstract method loadClass() to load
  44.  * the referenced classes.<p>
  45.  * <pre>
  46.  *     ClassLoader loader = new NetworkClassLoader(host, port);
  47.  *      Object main = loader.loadClass("Main").newInstance();
  48.  *    ....
  49.  * </pre>
  50.  *
  51.  * The NetworkClassLoader subclass must define the method loadClass() to 
  52.  * load a Class from the network. Once it has downloaded the bytes
  53.  * that make up the Class it should use the method defineClass() to create a Class
  54.  * instance. A sample implementation could be:
  55.  * <pre>
  56.  *    class NetworkClassLoader {
  57.  *        String host;
  58.  *        int port;
  59.  *        Hashtable cache = new Hashtable();
  60.  *
  61.  *        private byte loadClassData(String name)[] {
  62.  *        // load the class data from the connection
  63.  *        ...
  64.  *        }
  65.  *
  66.  *        public synchronized Class loadClass(String name) {
  67.  *            Class c = cache.get(name);
  68.  *        if (c == null) {
  69.  *            byte data[] = loadClassData(name);
  70.  *            cache.put(name, defineClass(data, 0, data.length));
  71.  *        }
  72.  *        return c;
  73.  *        }
  74.  *    }
  75.  * </pre>
  76.  * @see        Class
  77.  * @version     1.29, 12/21/95
  78.  * @author    Arthur van Hoff
  79.  */
  80.  
  81. public abstract class ClassLoader {
  82.     /**
  83.      * Constructs a new Class loader and initializes it.
  84.      */
  85.     protected ClassLoader() {
  86.     SecurityManager security = System.getSecurityManager();
  87.     if (security != null) {
  88.         security.checkCreateClassLoader();
  89.     }
  90.     init();
  91.     }
  92.  
  93.     /**
  94.      * Resolves the specified name to a Class. The method loadClass() is 
  95.      * called by the virtual machine.
  96.      * As an abstract method, loadClass() must be defined in a subclass of 
  97.      * ClassLoader. By using a Hashtable, you can avoid loading the same 
  98.      * Class more than once. 
  99.      * @param    name    the name of the desired Class
  100.      * @param resolve true if the Class needs to be resolved
  101.      * @return        the resulting Class, or null if it was not found.
  102.      * @exception ClassNotFoundException 
  103.      *                         Cannot find a definition for the class
  104.      * @see        java.util.Hashtable
  105.      */
  106.     protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException;
  107.  
  108.     /**
  109.      * Converts an array of bytes to an instance of class Class. Before the
  110.      * Class can be used it must be resolved.
  111.      * @param    data    the bytes that make up the Class
  112.      * @param    offset    the start offset of the Class data
  113.      * @param    length    the length of the Class data
  114.      * @return        the Class object which was created from the data.
  115.      * @exception ClassFormatError If the data does not contain a valid 
  116.      * Class.
  117.      * @see         ClassLoader#loadClass
  118.      * @see         ClassLoader#resolveClass
  119.      */
  120.     protected native final Class defineClass(byte data[], int offset, int length);
  121.  
  122.     /**
  123.      * Resolves classes referenced by this Class. This must be done before the
  124.      * Class can be used. Class names referenced by the resulting Class are
  125.      * resolved by calling loadClass().
  126.      * @param    c    the Class to be resolved
  127.      * @see         ClassLoader#defineClass
  128.      */
  129.     protected native final void resolveClass(Class c);
  130.  
  131.     /**
  132.      * Loads a system Class. A system Class is a class with the
  133.      * primordial Class loader (which is null).
  134.      * @param name the name of the system Class
  135.      * @exception NoClassDefFoundError If the Class is not found.
  136.      * @exception ClassNotFoundException 
  137.      *                         Cannot find a definition for the class
  138.      */
  139.     protected native final Class findSystemClass(String name) throws ClassNotFoundException;
  140.  
  141.     /**
  142.      * Initializes the Class loader.
  143.      */
  144.     private native void init();
  145. }
  146.